# data_generator.py
from faker import Faker
import random
import time


class PurchaseExchangeDataGenerator:
    def __init__(self, seed=42):
        self.fake = Faker()
        self.fake.seed_instance(seed)
        random.seed(seed)

        # 配置参数
        self.states = [0, 1, 2, 3, 4, 5, 6, 100]
        self.state_weights = [0.1, 0.15, 0.15, 0.1, 0.15, 0.15, 0.15, 0.05]
        self.currencies = {
            "USD": {"estimate": (6.3, 7.2), "purchase": (6.3, 7.2)},
            "HKD": {"estimate": (0.80, 0.90), "purchase": (0.80, 0.90)},
            "EUR": {"estimate": (7.5, 8.5), "purchase": (7.5, 8.5)},
            "JPY": {"estimate": (0.05, 0.06), "purchase": (0.05, 0.06)}
        }

    def generate_single_record(self):
        """生成单条购汇批次记录"""
        # 基础信息
        company_id = random.randint(1, 1000)
        plan_id = random.randint(1, 500)
        plan_name = self.fake.text(max_nb_chars=200).replace('\n', ' ').strip()
        batch_date = self.fake.date_between(start_date='-1y', end_date='today').strftime('%Y-%m-%d')
        tran_batch_no = f"BATCH-{batch_date.replace('-', '')}-{self.fake.unique.random_number(digits=8)}"
        payment_account_id = random.randint(1, 100)

        # 账户信息
        pay_act_no = f"622202{self.fake.random_number(digits=13, fix_len=True)}"
        payee_act_no = f"623052{self.fake.random_number(digits=13, fix_len=True)}"

        # 金额计算
        total_amt = round(random.uniform(1000, 1000000), 15)
        currency = random.choice(list(self.currencies.keys()))
        rate_range = self.currencies[currency]
        estimate_rate = round(random.uniform(*rate_range["estimate"]), 15)
        purchase_rate = round(estimate_rate * random.uniform(0.98, 1.02), 15)
        pay_amt = round(total_amt * purchase_rate, 15)

        # 构建记录
        return {
            "company_id": company_id,
            "plan_id": plan_id,
            "plan_name": plan_name,
            "date": batch_date,
            "tran_batch_no": tran_batch_no,
            "payment_account_id": payment_account_id,
            "pay_act_no": pay_act_no,
            "payee_act_no": payee_act_no,
            "total_amt": total_amt,
            "total_cost": round(total_amt * random.uniform(0.9, 1.1), 15),
            "total_fee": round(total_amt * random.uniform(0.001, 0.05), 15),
            "total_tax_cny": round(total_amt * random.uniform(0.05, 0.15), 15),
            "currency": currency,
            "estimate_rate": estimate_rate,
            "purchase_rate": purchase_rate,
            "pay_amt": pay_amt,
            "total_cnt": random.randint(1, 100),
            "state": random.choices(self.states, weights=self.state_weights, k=1)[0],
            "sell_amount": round(total_amt * random.uniform(1.0, 1.2), 15)
        }

    def generate_batch_records(self, num_records):
        """生成多条购汇批次记录"""
        records = []
        for _ in range(num_records):
            record = self.generate_single_record()

            # 处理时间字段的逻辑关系
            create_time = int(time.time()) - random.randint(0, 365 * 24 * 3600)
            update_time = create_time + random.randint(0, 30 * 24 * 3600)

            record["create_time"] = create_time
            record["update_time"] = update_time

            if record["state"] in (4, 5, 6):
                record["finished_time"] = update_time + random.randint(0, 3 * 24 * 3600)
            else:
                record["finished_time"] = 0

            if record["state"] == 100:
                record["delete_time"] = update_time + random.randint(1, 7 * 24 * 3600)
            else:
                record["delete_time"] = 0

            records.append(record)
        return records

# 使用示例
if __name__ == "__main__":
    print("测试")
    test_data = PurchaseExchangeDataGenerator().generate_batch_records(1)
    print(test_data)